package com.zhangkunhao.notes.view;

import com.zhangkunhao.notes.controller.RegisterListener;
import com.zhangkunhao.notes.entity.DO.User;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.util.List;

/**
 * 注册页面
 *
 * @author 张坤浩
 */
public class RegisterView extends JFrame {

    public JFrame loginFrame;
    public List<User> users;

    public RegisterView(JFrame frame, List<User> users) {
        this.loginFrame = frame;
        this.users = users;
    }

    public void reShow() {

        //创建JFrame实例
        JFrame registerFrame = new JFrame("欢迎注册");

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 443, 300);
        //设置窗口居中显示
        setLocationRelativeTo(null);
        //设置窗体宽高
        registerFrame.setSize(600, 400);
        //设置窗体禁止调节大小
        registerFrame.setResizable(true);

        JTextField passwordText;
        JTextField nameText;

        //创建面板
        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel label = new JLabel("欢迎来到注册页面!");
        label.setBounds(280, 0, 250, 25);
        label.setFont(new Font("微软雅黑", Font.BOLD, 25));
        label.setForeground(Color.red);
        contentPane.add(label);
        //关闭窗口结束程序
        registerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //显示窗口
        registerFrame.setVisible(true);

        //添加面板
        registerFrame.add(contentPane);
        //设置可见
        registerFrame.setVisible(true);

        JLabel userNameLabel = new JLabel("用户名:");
        userNameLabel.setBounds(112, 93, 43, 15);
        contentPane.add(userNameLabel);

        nameText = new JTextField();
        nameText.setBounds(151, 90, 160, 21);
        contentPane.add(nameText);
        nameText.setColumns(10);

        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setBounds(111, 134, 43, 15);
        contentPane.add(passwordLabel);

        passwordText = new JTextField();
        passwordText.setBounds(151, 131, 160, 21);
        contentPane.add(passwordText);
        passwordText.setColumns(10);
        //保存
        JButton saveBtn = new JButton("保存");
        saveBtn.setBounds(151, 180, 74, 23);
        contentPane.add(saveBtn);
        //取消
        JButton cancelButton = new JButton("取消");
        cancelButton.setBounds(237, 180, 74, 23);
        contentPane.add(cancelButton);

        RegisterListener registerListener = new RegisterListener(registerFrame, nameText,
                passwordText, contentPane, loginFrame);
        //执行controller层，绑定事件
        saveBtn.addActionListener(registerListener);

        cancelButton.addActionListener(registerListener);
    }
}
